package com.windaka.suizhi.utils;


import org.springframework.util.ResourceUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * 下载工具类
 * <li>
 * <p>
 * 注意：调用该方法的Controller不要有@Render等操作
 * <strong>示例: 返回下载流给前端</strong> <br>
 * DownLoadUtils.downLoadFile(response, filePath, downName);
 * 结果: 返回HttpServletResponse流给前端 下载文件
 * </p>
 * </li>
 * <li>
 * <p>
 * 注意：调用该方法的Controller不要有@Render等操作
 * <strong>示例2: 返回下载流给前端 从SpringBoot文件中</strong> <br>
 * DownloadUtils.downLoadFile(response, filePath, downName);
 * 结果: 返回HttpServletResponse流给前端 下载文件
 * </p>
 * </li>
 * @author wzk
 * @date 14:38 2022/7/25
**/
public class DownloadUtils {

    private DownloadUtils() {}

    private static final String CONTENT_TYPE = "application/octet-stream;charset=UTF-8";

    private static final String CONTENT_DISPOSITION = "Content-disposition";

    private static final String ATTACHMENT = "attachment;filename=";

    private static final String  UTF_8 = "UTF-8";

    /**
     * <li>
     * <p>
     * 注意：调用该方法的Controller不要有@Render等操作
     * <strong>示例1: 返回下载流给前端</strong> <br>
     * DownloadUtils.downLoadFile(response, filePath, downName);
     * 结果: 返回HttpServletResponse流给前端 下载文件
     * </p>
     * </li>
     **/
    public static void downLoadFile(HttpServletResponse response, String filePath, String downName) throws IOException {
        try (FileInputStream in = new FileInputStream(filePath);
             ServletOutputStream out = response.getOutputStream()) {
            response.setContentType(CONTENT_TYPE);
            response.setHeader(CONTENT_DISPOSITION, ATTACHMENT + URLEncoder.encode(downName, UTF_8));
            int len;
            byte[] buffer = new byte[1024];
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } finally {
            response.flushBuffer();
        }
    }

    /**
     * <li>
     * <p>
     * 注意：调用该方法的Controller不要有@Render等操作
     * <strong>示例2: 返回下载流给前端 从SpringBoot文件中</strong> <br>
     * DownloadUtils.downLoadFile(response, filePath, downName);
     * 结果: 返回HttpServletResponse流给前端 下载文件
     * </p>
     * </li>
     **/
    public static void downLoadSpringBootFile(HttpServletResponse response, String filePath, String downName) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(ResourceUtils.getFile(filePath));
            ServletOutputStream out = response.getOutputStream()) {
            response.setContentType(CONTENT_TYPE);
            response.setHeader(CONTENT_DISPOSITION, ATTACHMENT + URLEncoder.encode(downName, UTF_8));
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fileInputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } finally {
            response.flushBuffer();
        }
    }

    /**
     * <li>
     * <p>
     * 注意：调用该方法的Controller不要有@Render等操作
     * <strong>示例1: 返回下载流给前端</strong> <br>
     * DownloadUtils.downLoadFile(response, inputStream, downName);
     * 结果: 返回HttpServletResponse流给前端 下载文件
     * </p>
     * </li>
     **/
    public static void downLoadFile(HttpServletResponse response, InputStream inputStream, String downName) throws IOException {
        try (ServletOutputStream out = response.getOutputStream()) {
            response.setContentType(CONTENT_TYPE);
            response.setHeader(CONTENT_DISPOSITION, ATTACHMENT + URLEncoder.encode(downName, UTF_8));
            int len;
            byte[] buffer = new byte[1024];
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } finally {
            response.flushBuffer();
        }
    }
}
